package day17.edu;

import org.junit.Test;

// Created: 17/09/2025, ChangJiang Ru
public class StringTest {
    @Test
    public void test1()
    {
        String s1 = "hello";
        String s2 = "hello";
        String s3 = "hello";
        String s4 = new String("hello");

        s2 = "world";

//        System.out.println(s2 == s3);
//        System.out.println(s3 == s4);
    }
    
    @Test
    public void test2()
    {
        String s1 = new String(); // char[] value = new char[0];
        String s2 = new String("abc"); // char[] value = new char[]{'a', 'b', 'c'};
    }
    
    @Test
    public void test3()
    {
        StringBuilder sBuilder = new StringBuilder();
        sBuilder.append("abc").append(1).append(0.3).append("123");
        System.out.println(sBuilder);

        StringBuilder sBuilder1 = new StringBuilder("abc");
        System.out.println(sBuilder1);
    }

    @Test
    public void test4()
    {
        StringBuilder sBuilder = new StringBuilder("hello");
        sBuilder.insert(2,1);
        System.out.println(sBuilder);
        
        StringBuilder sBuilder1 = sBuilder.reverse();
        System.out.println(sBuilder1);
        System.out.println(sBuilder);
        System.out.println(sBuilder == sBuilder1);
    }
    
    @Test
    public void test5()
    {
        StringBuilder sBuilder = new StringBuilder("hello");
        sBuilder.setLength(2);

        System.out.println(sBuilder);
        System.out.println(sBuilder.length());
        sBuilder.append("c");
        System.out.println(sBuilder);

        System.out.println("======");
        sBuilder.setLength(10);
        System.out.println(sBuilder);

        System.out.println("=======");
        System.out.println(sBuilder.charAt(6) == 0);
    }
    
    @Test
    public void test6()
    {
        long startTime = 0L;
        long endTime = 0L;

        String text = "";
        StringBuilder builder = new StringBuilder("");
        StringBuffer buffer = new StringBuffer("");

        // Start compare
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 2000000; i++) {
            builder.append(String.valueOf(i));
        }
        endTime = System.currentTimeMillis();
        System.out.println("Input the builder the time is : " + (endTime - startTime));

        startTime = System.currentTimeMillis();
        for (int i = 0; i < 2000000; i++) {
            buffer.append(String.valueOf(i));
        }
        endTime = System.currentTimeMillis();
        System.out.println("Input the buffer the time is : " + (endTime - startTime));

        startTime = System.currentTimeMillis();
        for (int i = 0; i < 20000; i++) {
            text = text + i;
        }
        endTime = System.currentTimeMillis();
        System.out.println("Input the String the time is : " + (endTime - startTime));
    }

    @Test
    public void test7()
    {
        String text = "hello";
        text = text + 1;
        System.out.println(text);
    }

    @Test
    public void test8()
    {
        String str = null;
        StringBuilder sb = new StringBuilder();

        sb.append(str);
        System.out.println(sb.length()); // 0

        System.out.println(sb); // null

        StringBuilder sb1 = new StringBuilder(str);
        System.out.println(sb1); // null
    }
    
    @Test
    public void test9()
    {
        
    }
}
